//
//  HSQScreeningButtonView.m
//  LiTianDecoration
//
//  Created by administrator on 2018/8/1.
//  Copyright © 2018年 administrator. All rights reserved.
//

#import "HSQScreeningButtonView.h"
#import "HSQCustomButton.h"

@interface HSQScreeningButtonView ()



@end

@implementation HSQScreeningButtonView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        // 综合按钮
        HSQCustomButton *comprehensive_btn = [self SetUpButtonWithTitle:@"综合"];
        [comprehensive_btn addTarget:self action:@selector(comprehensive_btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
        comprehensive_btn.selected = YES;
        self.comprehensive_btn = comprehensive_btn;
        
        // 销量按钮
        HSQCustomButton *sales_btn = [self SetUpButtonWithTitle:@"销量"];
        [sales_btn addTarget:self action:@selector(sales_btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.sales_btn = sales_btn;
        
        // 价格按钮
        HSQCustomButton *price_btn = [self SetUpButtonWithTitle:@"价格"];
        [price_btn addTarget:self action:@selector(price_btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.price_btn = price_btn;
        
        // 筛选按钮
        HSQCustomButton *Screening_btn = [self SetUpButtonWithTitle:@"筛选"];
        [Screening_btn addTarget:self action:@selector(Screening_btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.Screening_btn = Screening_btn;
        
    }
    
    return self;
}

/**
 * @brief 创建按钮
 */
- (HSQCustomButton *)SetUpButtonWithTitle:(NSString *)title{
    
    HSQCustomButton *button = [HSQCustomButton buttonWithType:(UIButtonTypeCustom)];
    
    [button setTitle:title forState:UIControlStateNormal];

    [button setTitleColor:RGB(131, 131, 131) forState:UIControlStateNormal];
    
    [button setTitleColor:RGB(255, 83, 63) forState:UIControlStateSelected];
    
    button.titleLabel.font = [UIFont systemFontOfSize:KLabelFont(14.0, 14.0)];
        
    [self addSubview:button];
    
    if (self.subviews.count == 1) // 综合按钮
    {
        [button setImage:KImageName(@"F29714FE-BAB1-48BE-AED0-4825E2E9BFB6") forState:(UIControlStateNormal)];
        
        [button setImage:KImageName(@"F29714FE-BAB1-48BE-AED0-4825E2E9BFB6") forState:(UIControlStateSelected)];
    }
    else if (self.subviews.count == 2) // 销量按钮
    {
        
    }
    else if (self.subviews.count == 3) // 价格按钮
    {
        [button setImage:KImageName(@"F29714FE-BAB1-48BE-AED0-4825E2E9BFB6") forState:(UIControlStateNormal)];
    }
    else if (self.subviews.count == 4) // 筛选按钮
    {
        [button setImage:KImageName(@"2B4072B2-E0E0-4834-8297-81D825E5CF66") forState:(UIControlStateNormal)];
    }
    
    if (self.subviews.count != 2)
    {
        button.alignmentType = Button_AlignmentStatusRight;
    }
    else
    {
        button.alignmentType = Button_AlignmentStatusNormal;
    }

    return button;
}

/**
 * @brief 综合按钮的点击
 */
- (void)comprehensive_btnClickAction:(HSQCustomButton *)sender{
    
    self.comprehensive_btn.selected = YES;
    
    self.sales_btn.selected = self.price_btn.selected = NO;
    
    if ([self.delegate respondsToSelector:@selector(ComprehensiveBtnClickAction:ComprehensiveBtn:)]) {
        
        [self.delegate ComprehensiveBtnClickAction:self ComprehensiveBtn:sender];
    }
}

/**
 * @brief 销量按钮的点击
 */
- (void)sales_btnClickAction:(HSQCustomButton *)sender{
    
    self.sales_btn.selected = YES;
    
    self.comprehensive_btn.selected = self.price_btn.selected = NO;
    
    if ([self.delegate respondsToSelector:@selector(SaleBtnClickAction:SaleBtn:)]) {
        
        [self.delegate SaleBtnClickAction:self SaleBtn:sender];
    }
}

/**
 * @brief 价格按钮的点击
 */
- (void)price_btnClickAction:(HSQCustomButton *)sender{
    
    self.price_btn.selected = YES;
    
    self.comprehensive_btn.selected = self.sales_btn.selected = NO;
    
    if ([self.delegate respondsToSelector:@selector(PriceBtnClickAction:PriceBtn:)]) {
        
        [self.delegate PriceBtnClickAction:self PriceBtn:sender];
    }
}

/**
 * @brief 筛选按钮的点击
 */
- (void)Screening_btnClickAction:(HSQCustomButton *)sender{
    
    if ([self.delegate respondsToSelector:@selector(ScreenBtnClickAction:ScreenBtn:)]) {
        
        [self.delegate ScreenBtnClickAction:self ScreenBtn:sender];
    }
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    // 设置按钮的frame
    NSUInteger btnCount = self.subviews.count;
    
    CGFloat btnW = self.width / btnCount;
    
    CGFloat btnH = self.height;
    
    for (int i = 0; i<btnCount; i++) {
        
        HSQCustomButton *btn = self.subviews[i];
        
        btn.mj_y = 0;
        
        btn.width = btnW;
        
        btn.mj_x = i * btnW;
        
        btn.height = btnH;
    }
}






@end
